<?php
function edit_bckcstudio($config,$param){
	$desc = trim($param['desccstudio']);
	if ($desc!=""){
		$db = new db($config);
		$que = "UPDATE `deskripsi` SET `deskripsi`='$desc' WHERE `page` = 'cstudio';";
		$db->exec($que);
		$_SESSION['editted'] = 'Sukses edit deskripsi CStudio';
	}
	header('Location: '.$config['home_url']."backend/cstudio");
}

function add_bckcstudioworks($config,$param){
	$works = trim($param['namaplus']);
	$slug = helper::slugging($works);
	//$desc = trim($param['descplus']);
	$desc = "";
	//echo $kutip;
	if ($works!=""){
		$db = new db($config);
		$que = "INSERT INTO `cstudio` (`works` ,`slug-works` ,`deskripsi`) VALUES (\"$works\", \"$slug\", \"$desc\");";
		$db->exec($que);
		if ($db->lastInsertId()>0)
			$_SESSION['editted'] = 'Sukses tambah works';
		else
			$_SESSION['editted'] = 'Gagal tambah works';
	}
	header('Location: '.$config['home_url']."backend/cstudio");
}

function add_bcksubworks($config,$param){
	$subw = trim($param['subplus']);
	$idworks = $param['idworks'];
	$slug = helper::slugging($subw);
	//$desc = trim($param['descplus']);
	$desc = "";
	//echo $kutip;
	if ($subw!=""){
		$db = new db($config);
		$que = "INSERT INTO `sub_works` (`idWorks` ,`subw`, `slug-subw`) VALUES ($idworks, \"$subw\", \"$slug\");";
		//echo $que; die();
		$db->exec($que);
		if ($db->lastInsertId()>0)
			$_SESSION['editted'] = "Sukses tambah sub-works : $subw";
		else
			$_SESSION['editted'] = "Gagal tambah sub-works : $subw";
	}
	header('Location: '.$config['home_url']."backend/cstudio");
}

function edit_bckcstudioworks($config,$param){
	$works = trim($param['namaed']);
	$slug = helper::slugging($works);
	//$desc = trim($param['descplus']);
	$desc = "";
	$idk = $param['id'];
	//echo $kutip." ".$idk;
	echo $works;
	if ($works!=""){
		$db = new db($config);
		$que = "UPDATE `cstudio` SET `works`=\"$works\", `slug-works`=\"$slug\", `deskripsi`=\"$desc\" WHERE `idCstudio` = $idk;";
		echo $que;
		$db->exec($que);
		$_SESSION['editted'] = 'Sukses edit works';
	}
	header('Location: '.$config['home_url']."backend/cstudio");
}

function edit_bcksubworks($config,$param){
	$subw = trim($param['subed']);
	$slug = helper::slugging($subw);
	//$desc = trim($param['descplus']);
	$desc = "";
	$idk = $param['id'];
	//echo $kutip." ".$idk;
	echo $works;
	if ($subw!=""){
		$db = new db($config);
		$que = "UPDATE `sub_works` SET `subw`=\"$subw\", `slug-subw`=\"$slug\" WHERE `idSubworks` = $idk;";
		echo $que;
		$db->exec($que);
		$_SESSION['editted'] = "Sukses edit sub works $subw";
	}
	header('Location: '.$config['home_url']."backend/cstudio");
}

function add_bckcstudioworksfoto($config,$param){
	$uploaddir = $config['image_path'].'cstudio'.DIRECTORY_SEPARATOR.'works'.DIRECTORY_SEPARATOR;
	$preddir = $config['image_path'].'cstudio'.DIRECTORY_SEPARATOR.'pre'.DIRECTORY_SEPARATOR;
	$idw = $param['idworks'];
	$ids = $param['idsubs'];
	$limitedext = array("jpg","jpeg","png","bmp");
	$ext = strrchr($_FILES['gambar']['name'],'.');
	$ext = strtolower($ext);
	$getExt = explode('.',$ext);
	$getName = explode('.',$_FILES['gambar']['name']);
	$getName = helper::slugging($getName[0]);
	$file_ext = $getExt[count($getExt)-1];
	$file_name = $idw."-".$ids."-".$getName.".".$file_ext;
	$uploadfile = $uploaddir.$file_name;
	if (!in_array($file_ext,$limitedext)) {
		$_SESSION['editted'] = "Tipe file salah<br />Tipe file yang diperbolehkan adalah jpg, jpeg, bmp, png.";
	} else {
		if (move_uploaded_file($_FILES['gambar']['tmp_name'], $uploadfile)) {
			// process for thumbnail image
			WideImage::load($uploadfile)->resize(500,300,'fill')->saveToFile($uploadfile);	//works
			WideImage::load($uploadfile)->resize(100,60,'fill')->saveToFile($preddir.$file_name);	//pre
			//WideImage::load($uploadfile)->crop('center','center',250,300)->saveToFile($preddir.$file_name.'.'.$file_ext);	//pre
			$_SESSION['editted'] = "Sukses upload gambar";
		} else {
			switch ($_FILES['gambar']['error']) {
				case 2:
					$_SESSION['editted'] = "Error: Ukuran gambar melebihi batas<br />Batas ukuran file adalah 1 MB";
					break;
				case 7:
					$_SESSION['editted'] = "Error: Tidak dapat disimpan dalam server";
					break;
				default:
					$_SESSION['editted'] = "Error: Unknown error ".$_FILES['gambar']['error'];
			}
		}
	}
	header('Location: '.$config['home_url']."backend/cstudio");
}

function del_bckcstudioworksfoto($config,$param){
	$img=$param['img'];
	$uploaddir = $config['image_path'].'cstudio'.DIRECTORY_SEPARATOR.'works'.DIRECTORY_SEPARATOR;
	$preddir = $config['image_path'].'cstudio'.DIRECTORY_SEPARATOR.'pre'.DIRECTORY_SEPARATOR;
	unlink($uploaddir.$img);
	unlink($preddir.$img);
	$_SESSION['editted'] = 'Sukses hapus foto';
	header('Location: '.$config['home_url']."backend/cstudio");
}

function del_bckcstudioworks($config){
	$idk = $config['ruri'][2];
	//hapus db
	$db = new db($config);
	$que = "DELETE FROM cstudio WHERE idCstudio=$idk";
	echo $que;
	$db->exec($que);
	//hapus gambar
	$uploaddir = $config['image_path'].'cstudio'.DIRECTORY_SEPARATOR.'works'.DIRECTORY_SEPARATOR;
	$preddir = $config['image_path'].'cstudio'.DIRECTORY_SEPARATOR.'pre'.DIRECTORY_SEPARATOR;
	$regex = $uploaddir.$idk."-*";
	$limitedext = array("jpg","jpeg","png","bmp");
	//FILTER
	foreach (glob($regex) as $files) {
		$getFile = explode(DIRECTORY_SEPARATOR,$files);
		$file = $getFile[count($getFile)-1];
		$getExt = explode('.',$file);
		$file_ext = $getExt[count($getExt)-1];
		if (in_array($file_ext,$limitedext)) {
			unlink($uploaddir.$file);
			unlink($preddir.$file);
		}
	}
	$_SESSION['editted'] = 'Sukses hapus works';
	header('Location: '.$config['home_url']."backend/cstudio");
}

function del_bcksubworks($config){
	$idc = $config['ruri'][2];
	$ids = $config['ruri'][3];
	//hapus db
	$db = new db($config);
	$que = "DELETE FROM sub_works WHERE idSubworks=$ids";
	echo $que;
	$db->exec($que);
	//hapus gambar
	$uploaddir = $config['image_path'].'cstudio'.DIRECTORY_SEPARATOR.'works'.DIRECTORY_SEPARATOR;
	$preddir = $config['image_path'].'cstudio'.DIRECTORY_SEPARATOR.'pre'.DIRECTORY_SEPARATOR;
	$regex = $uploaddir.$idc."-".$ids."-*";
	$limitedext = array("jpg","jpeg","png","bmp");
	//FILTER
	foreach (glob($regex) as $files) {
		$getFile = explode(DIRECTORY_SEPARATOR,$files);
		$file = $getFile[count($getFile)-1];
		$getExt = explode('.',$file);
		$file_ext = $getExt[count($getExt)-1];
		if (in_array($file_ext,$limitedext)) {
			unlink($uploaddir.$file);
			unlink($preddir.$file);
		}
	}
	$_SESSION['editted'] = 'Sukses hapus sub works';
	header('Location: '.$config['home_url']."backend/cstudio");
}
